package com.chinafree.business.mapping;

import com.chinafree.business.model.param.*;
import com.chinafree.business.model.result.*;
import com.chinafree.model.po.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

@Mapper
public interface UserMapping {

    UserMapping instance = Mappers.getMapper(UserMapping.class);

    BaseUserResult maUserToBaseUserResult(MaUser maUser);

    UserWorkResult maWorkToUserWorkResult1(MaUserWork value);

    UserEducationResult maUserEducationToUserEducationResult1(MaUserEducation value);

    UserSkillResult maSkillToUserSkillResult1(MaUserSkill value);

    UserFileResult maFileToUserFileResult1(MaUserFile value);

    List<BaseUserResult> maUserToBaseUserResults(List<MaUser> maUser);

    List<UserWorkResult> maWorkToUserWorkResult(List<MaUserWork> maUserWorks);

    List<UserEducationResult> maEducationToUserEducationResult(List<MaUserEducation> maUserEducations);

    List<UserSkillResult> maSkillToUserSkillResult(List<MaUserSkill> maUserSkills);

    List<UserFileResult> maFileToUserFileResult(List<MaUserFile> maUserFiles);

    @Mappings({
            @Mapping(target = "value", source = "code"),
            @Mapping(target = "label", source = "name"),
            @Mapping(target = "expand", expression = "java(buildExpand(fndProvCityArea.getLevel()))")
    })
    ProvCityResult fndProvCityAreaToProvCityResult(FndProvCityArea fndProvCityArea);

    default Boolean buildExpand(Integer num) {
        if (num == 1) {
            return false;
        } else {
            return false;
        }
    }

    List<ProvCityResult> fndProvCityAreaToProvCityResult(List<FndProvCityArea> fndProvCityAreas);

    @Mapping(target = "born", expression = "java(timeStampTOLocalDateTime(body.getBorn()))")
    MaUser BaseUserParamToMaUser(BaseUserParam body);

    @Mappings({
            @Mapping(target = "startTime", expression = "java(timeStampTOLocalDateTime(userEducationParam.getStartTime()))"),
            @Mapping(target = "endTime", expression = "java(timeStampTOLocalDateTime(userEducationParam.getEndTime()))")
    })
    MaUserEducation UserEducationParamToEntity(UserEducationParam userEducationParam);

    default List<MaUserEducation> UserEducationParamsToEntities(List<UserEducationParam> userEducationParams, Long id) {
        ArrayList<MaUserEducation> maUserEducations = new ArrayList<>();
        for (UserEducationParam userEducationParam : userEducationParams) {
            MaUserEducation maUserEducation = UserEducationParamToEntity(userEducationParam);
            maUserEducation.setUserId(id);
            maUserEducation.setId(userEducationParam.getId());
            maUserEducations.add(maUserEducation);
        }
        return maUserEducations;
    }

    @Mappings({
            @Mapping(target = "startTime", expression = "java(timeStampTOLocalDateTime(userEducationParam.getStartTime()))"),
            @Mapping(target = "endTime", expression = "java(timeStampTOLocalDateTime(userEducationParam.getEndTime()))")
    })
    MaUserWork UserWorkParamToEntity(UserWorkParam userEducationParam);

    default List<MaUserWork> UserWorkParamsToEntities(List<UserWorkParam> userWorkParams, Long id) {
        ArrayList<MaUserWork> maUserWorks = new ArrayList<>();
        for (UserWorkParam userWorkParam : userWorkParams) {
            MaUserWork maUserWork = UserWorkParamToEntity(userWorkParam);
            maUserWork.setUserId(id);
            maUserWork.setId(userWorkParam.getId());
            maUserWorks.add(maUserWork);
        }
        return maUserWorks;
    }

    MaUserSkill UserSkillParamToEntity(UserSkillParam userSkillParam);

    default List<MaUserSkill> UserSkillParamsToEntities(List<UserSkillParam> userSkillParams, Long id) {
        ArrayList<MaUserSkill> maUserSkills = new ArrayList<>();
        for (UserSkillParam userSkillParam : userSkillParams) {
            MaUserSkill maUserSkill = UserSkillParamToEntity(userSkillParam);
            maUserSkill.setUserId(id);
            maUserSkill.setId(userSkillParam.getId());
            maUserSkills.add(maUserSkill);
        }
        return maUserSkills;
    }

    MaUserFile UserFileParamToEntity(FilesUserParam filesUserParam);

    List<MaUserFile> UserFileParamsToEntities(List<FilesUserParam> body);

    DeliveryResult maUserDeliveryToDeliveryResult(MaUserDelivery maUserDelivery);

    List<DeliveryResult> maUserDeliveriesToDeliveryResults(List<MaUserDelivery> records);

    CompanyResult maCompanyToResult(MaCompany maCompany);

    List<CompanyResult> maCompanyToResults(List<MaCompany> maCompany);

    JobResult maJobToResult(MaJob maJob);

    MaJob jobParamToMaJob(JobParam jobParam);

    MaCompany companyParamToMaCompany(CompanyParam body);

    default LocalDateTime timeStampTOLocalDateTime(Long time) {
        if (time == null) {
            return null;
        }
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneOffset.of("+8"));
    }
}
